﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RMS.Utils.MVP;
using RMS.Web.Shell.View;
using RMS.DataAccess.Entities;
using RMS.DataAccess.Repositories;
using RMS.Utils.Dicom;
using RMS.Utils.Histogram;
using RMS.DataAccess.Common;

namespace RMS.Web.Shell.Presenter
{
    public class SearchInImagesPresenter : Presenter<ISearchInImagesView>
    {
        RadiologyImageRepository _radiologyImageRepository;
        IDicomBase _dicomBase;
        IHistogram _histogram;
        byte[] histogrambyte;

        public SearchInImagesPresenter(RadiologyImageRepository radiologyImageRepository, IDicomBase dicomBase, IHistogram histogram)
        {
            _radiologyImageRepository = radiologyImageRepository;
            _dicomBase = dicomBase;
            _histogram = histogram;
        }

        public EntityResult<RadiologyImage> SearchIninImgs(string path, string extension)
        {
            if (extension.ToLower().Equals(".jpg") || extension.ToLower().Equals(".png"))
            {
                histogrambyte = _histogram.GetHistogram(path);
            }
            else if (extension.ToLower().Equals(".dcm"))
            {
                _dicomBase.FilePath = path;
                _dicomBase.SaveImage(path + "_2");
                histogrambyte = _histogram.GetHistogram(path + "_2");
            }
            else
                return new EntityResult<RadiologyImage>();

            return GetImages();
        }
        private EntityResult<RadiologyImage> GetImages()
        {
            if (histogrambyte.Length != 0)
            {
                return _radiologyImageRepository.Find(item => item.Histogram = histogrambyte);

            }
            else
                return new EntityResult<RadiologyImage>();
        }
    }
}
